package com.example.bluetooth_test;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class Control extends AppCompatActivity {

    public static TextView mesText;
    ImageButton btUp, btDown, btLeft, btRight;

    public static ClientThread mClientThread;



//    private Handler handler=null;
//    private MyAPP myAPP = null;
//    @SuppressLint("HandlerLeak")
//    public static Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//                    byte[] readBuffer = (byte[]) msg.obj;
//                    String tempMsg = new String(readBuffer, 0, msg.arg1);
//                    Log.d("Mainactivity", "message"+tempMsg);
//            }
//    };

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.controlled);

//        handler = new Handler();
//        myAPP = (MyAPP)getApplication();
//        myAPP.setHandler(handler);

        mesText = findViewById(R.id.msgText);
        btUp = findViewById(R.id.buttonUp);
        btDown = findViewById(R.id.buttonDown);
        btLeft = findViewById(R.id.buttonLeft);
        btRight = findViewById(R.id.buttonRight);

//        mesText.setText(getIntent().getStringExtra("Message"));

        btUp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                turnUp();
                return true;
            }
        });

        btRight.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                turnRight();
                return true;
            }
        });

        btLeft.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                turnLeft();
                ;
                return true;
            }
        });

        btDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                turnDown();
                return true;
            }
        });

//        IntentFilter intentFilter = new IntentFilter(MainActivity.maction);
//        registerReceiver(receiver, intentFilter);
    }

//    final class MyHandler extends Handler {
//
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == Constant.STATE_MESSAGE_RECEIVED) {
//                String mesg = (String)msg.obj;
//                mesText.setText(mesg);
//            }
//        }
//    }



    private void turnUp() {
        if (mClientThread != null) {
            String front = "<FRONT>";
            mClientThread.write(front.getBytes());
        }
    }

//            Intent intent = new Intent(TURN_UP);
//            intent.putExtra("turnup", "<FRONT>");
//            sendBroadcast(intent);
//            finish();
//            String front = "<FRONT>";
//            Message message = handler.obtainMessage();
//            message.what = Constant.STATE_FRONT;
//            message.obj = front;
//            handler.sendMessage(message);

    private void turnDown() {
        if (mClientThread != null) {
            String back = "<BACK>";
            mClientThread.write(back.getBytes());
        }
    }

    private void turnRight() {
        if (mClientThread != null) {
            String right = "<RIGHT>";
            mClientThread.write(right.getBytes());
        }
    }

    private void turnLeft() {
        if (mClientThread != null) {
            String left = "<LEFT>";
            mClientThread.write(left.getBytes());
        }
    }

//    BroadcastReceiver receiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            mesText.setText(intent.getStringExtra("Message"));
//        }
//    };


//        try {
//            byte[] bytes = new byte[1024];
//            mSocket.getInputStream().read(bytes);
//            mesText.setText(bytes.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


//    Handler mhandler = new Handler(new Handler.Callback() {
//        @Override
//        public boolean handleMessage(Message msg) {
//            switch (msg.what) {
//                case Constant.STATE_MESSAGE_RECEIVED:
//                    byte[] readBuffer = (byte[])msg.obj;
//                    String mMsg = new String(readBuffer, 0, msg.arg1);
//                    mesText.setText(mMsg);
//                    break;
//            }
//            return true;
//        }
//    });
}
